package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import fgame.GameState;
	
	import flare.materials.filters.AlphaMaskFilter;
	import flare.materials.filters.ColorMatrixFilter;
	import flare.materials.filters.ColorTransformFilter;
	import flare.materials.filters.FogFilter;
	import flare.materials.filters.VertexColorFilter;
	
	import model.ConfigUnitHelper;
	import model.UnitKey;
	import model.config.Ability;
	import model.config.AbilityData;
	import model.user.User;
	
	import ricky.plutonium.Application;
	
	import ui.UIManager;
	import ui.Welcome;
	import ui.loading.Loading;
	
	public class NetGame extends Sprite
	{
		public function NetGame()
		{
			super();
			
			Debug.isDebug=CLocale.getValue("isDebug")=="true";
			
			if(stage)
			{
				stage.scaleMode=StageScaleMode.NO_SCALE;
				stage.align=StageAlign.TOP_LEFT;	
				stage.frameRate=60;
				init();
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE,init);
			}
		}
		
		
		public function get loading():Loading
		{
			if(!_loading)
			{
				_loading=new Loading();
				_loading.x=(this.stage.stageWidth-800)/2
				_loading.y=this.stage.stageHeight-200;
			}
			return _loading;
		}

		public function set loading(value:Loading):void
		{
			_loading = value;
		}

		public function loadConfig(config:Object):void
		{
			for (var p:String in config)
			{
				if(p=="unit")
				{
					var unit:Object=config[p];
					for(var key:String in unit)
					{
						ConfigUnitHelper.initUnit(key,unit[key].toString());
					}
					
				}else if(p=="ability")
				{
					var ability:Object=config[p];
					var key1:String=UnitKey.KeyAbilityData;
					ConfigUnitHelper.init(key1,ability[key1].toString(),AbilityData,"abDataID");

					var key2:String=UnitKey.KeyAbility;
					ConfigUnitHelper.init(key2,ability[key2].toString(),Ability,"abilityID");
					
					var alldata:Dictionary=ConfigUnitHelper.getDicConfigByKey(key2);
					var abItem:Dictionary=ConfigUnitHelper.getDicConfigByKey(key1);
					var itemdata:Array,item:Object;
					for each(var ab:Object in alldata)
					{
						if(ab.data)
							itemdata=ab.data.split(","); 
						else
							itemdata=[];
						ab.dataArr=[];
						for each(var abName:String in itemdata)
						{
							item=abItem[abName];
							if(item)
								ab.dataArr.push(item);
						}
					}
				}else if(p=="native")
				{
					Application.native=config[p];
				}
			}
			
			Debug.traceObj(this,"native:"+Application.native+",资源url:"+Application.internetDomain,"全局配置");
		}
		
		/**
		 * 初始化
		 */
		public function init(e:Event=null):void
		{
			if(Debug.isDebug)
			{
				var adds:String=CLocale.getValue("debugerAdress");
				Debug.init(this,adds);
			}
			
			var user:Object=Application.parameters[ApplicationParametersAttributes.userInfo];

			User.userName=user["userName"];

			this.removeEventListener(Event.ADDED_TO_STAGE,init);

			GameState.inst.init(this);
			setInfo("初使化游戏状态");
			
			addChild(new Stats());
//			loading.parent.removeChild(loading);
			welcome();
			
			UIManager.inst.game=this;
			UIManager.inst.loading=this.loading;
			this.addChild(UIManager.inst.uiLayer);
			
			GameState.inst.running=true;
		}
		
		/**
		 * 显示欢迎界面
		 */
		public function welcome():void
		{
			var w:Welcome=new Welcome();
			w.onComplete=function():void
			{
				w.parent.removeChild(w);
				var host:String= Application.parameters[ApplicationParametersAttributes.HOST];
				var port:String=Application.parameters[ApplicationParametersAttributes.PORT];
				MsgHelper.connect(host,int(port));
			}
			GameState.inst.root.addChild(w);
		}
		
		public function setInfo(value:String):void
		{
			if(loading)
				loading.info=value;
			else
				trace(value);
		}
		private var _loading:Loading;
	}
}